﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.produceDAL.Stock;
using dhDataAcess.dataAcess.stockDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.produceLinq.StockLinq;
using dhDataAcess.sysDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters ;

namespace dhSystemBLL.produceBLL
{
   public  class orderProduceListBLL
    {
       private V_ordeProduceListTableAdapter V_orderProduceListAdapter = null;
       private V_ordeProduceListTableAdapter VorderProduceListAdapter
       {
           get
           {
               if (V_orderProduceListAdapter == null) V_orderProduceListAdapter = new V_ordeProduceListTableAdapter();
               return V_orderProduceListAdapter;

           }
       }
        private orderProduceListTableAdapter _orderProduceListAdapter = null;
        private orderProduceListTableAdapter orderProduceListAdapter
        {
            get
            {
                if (_orderProduceListAdapter == null) _orderProduceListAdapter = new orderProduceListTableAdapter();
                return _orderProduceListAdapter;

            }
        }
      
        /// <summary>
        /// 查询生产通知单表
        /// </summary>
        /// <returns></returns>

        public stockDataSet.orderProduceListDataTable getorderProduceListAll()
        {
            try
            {
                return orderProduceListAdapter.GetDataorderProduceListAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据生产单号查询生产通知单
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public stockDataSet.orderProduceListDataTable getorderProduceListAllByproduceNo(string produceNo)
        {
            try
            {
                return orderProduceListAdapter.GetDataorderProduceListByproduceNo(produceNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据漂染单号查询生产通知单
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public stockDataSet.orderProduceListDataTable getorderProduceListAllByorderDetailNo(string orderDetailNo)
        {
            try
            {
                return orderProduceListAdapter.GetDataorderProduceListByorderDetailNo  (orderDetailNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 视图查询
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public orderDataSet .V_ordeProduceListDataTable  getVorderProduceList()
        {
            try
            {
                return VorderProduceListAdapter.GetDataV_orderproduceAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 视图查询根据订单号
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public orderDataSet.V_ordeProduceListDataTable getVorderProduceListByproduceNo(string orderNo)
        {
            try
            {
                return VorderProduceListAdapter.GetDataByorderNo(orderNo) ;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 视图查询根据订单号
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public orderDataSet.V_ordeProduceListDataTable getVByproduceNo(string orderNo)
        {
            try
            {
                return VorderProduceListAdapter.GetDataproduceByorderNo (orderNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 视图查询根据生产单号
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public orderDataSet.V_ordeProduceListDataTable getproduceByproduceNo(string produceNo)
        {
            try
            {
                return VorderProduceListAdapter.GetDataV_orderproduceByproduceNo(produceNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int orderProduceListInsert(orderProduceListModel  row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderProduceListDAL orderProduceList = new orderProduceListDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                orderProduceList.orderProduceListInsert(row);
                string orderSerial = basicSql.getOrderSerail("生产单号");
                basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "生产单号");
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个生产单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int orderProduceListUpdate(orderProduceListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderProduceListDAL orderProduceList = new orderProduceListDAL(tn, conn);
                orderProduceList.orderProduceListUpdate (row);
                tn.Commit();
                return i;

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

        public int orderProduceListDel(string produceNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderProduceListDAL orderProduceList = new orderProduceListDAL(tn, conn);
                orderProduceList.orderProduceListDel(produceNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

    }
}
